package com.movie.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.movie.datacomm.common.CommResp;
import com.movie.datacomm.model.Comment;
import com.movie.datacomm.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    CommentService commentService;


    @RequestMapping(value = "/getCurrentPageComment", method = RequestMethod.GET)
    @ResponseBody
    public CommResp getCurrentPageComment(int currentPage, int pageSize, @Nullable String input){

        Page<Comment> page = new Page<>(currentPage, pageSize);

        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(input)){
            wrapper.like("comment_name", input);
        }
        wrapper.orderByAsc("comment_id");

        page = commentService.page(page, wrapper);
        return CommResp.okList(page.getRecords(), page.getTotal());
    }

    @RequestMapping(value = "/updateCommentInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommResp updateCommentInfo(@RequestBody Comment comment){
        boolean result = commentService.updateById(comment);
        if(result) {
            return CommResp.ok();
        }else{
            return CommResp.fail("更新失败");
        }
    }

    @RequestMapping(value = "/addCommentInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommResp addCommentInfo(@RequestBody Comment comment){

        boolean result = commentService.save(comment);
        if(result) {
            return CommResp.ok();
        }else{
            return CommResp.fail("操作失败");
        }
    }

    @RequestMapping(value = "/deleteCurrentComment", method = RequestMethod.POST)
    @ResponseBody
    public CommResp deleteCommentInfo(@RequestBody Comment comment){

        boolean result = commentService.removeById(comment.getCommentId());
        if(result) {
            return CommResp.ok();
        }else{
            return CommResp.fail("删除失败");
        }
    }

    //TODO
    @RequestMapping(value = "/passCurrentComment", method = RequestMethod.POST)
    @ResponseBody
    public CommResp passCurrentComment(@RequestBody Comment comment){

        boolean result = commentService.removeById(comment.getCommentId());
        if(result) {
            return CommResp.ok();
        }else{
            return CommResp.fail("删除失败");
        }
    }

}
